//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using EntLibContrib.Resource.Properties;

namespace EntLibContrib.Resource.Instrumentation
{
	/// <summary>
	/// The instrumentation gateway when no instances of the objects from the block are involved.
	/// </summary>
	[EventLogDefinition("Application", EventLogSourceName)]
	[CustomFactory(typeof(DefaultResourceEventLoggerCustomFactory))]
	public class DefaultResourceEventLogger : InstrumentationListener
	{
		#region Constants
		/// <summary>
		/// Event log source name
		/// </summary>
		public const string EventLogSourceName = ResourceInstrumentationListener.EventLogSourceName;
		#endregion

		#region Fields
		readonly IEventLogEntryFormatter eventLogEntryFormatter;
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="DefaultResourceEventLogger"/> class, specifying whether 
		/// logging to the event log and firing WMI events is allowed.
		/// </summary>
		/// <param name="eventLoggingEnabled"><b>true</b> if writing to the event log is allowed, <b>false</b> otherwise.</param>
		/// <param name="wmiEnabled"><b>true</b> if firing WMI events is allowed, <b>false</b> otherwise.</param>
		public DefaultResourceEventLogger (bool eventLoggingEnabled, bool wmiEnabled) : base((string)null, false, eventLoggingEnabled, wmiEnabled, null)
		{
			eventLogEntryFormatter = new EventLogEntryFormatter(InternalResources.ResourceBlockName);
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Logs the occurrence of a configuration error for the Enterprise Library Resource Application Block through the 
		/// available instrumentation mechanisms.
		/// </summary>
		/// <param name="instanceName">Name of the <see cref="ResourceManager"/> instance in which the configuration error was detected.</param>
		/// <param name="exception">The exception raised for the configuration error.</param>
		public void LogConfigurationError(string instanceName, Exception exception)
		{
			if (exception == null)
				throw new ArgumentNullException("exception");

			if (WmiEnabled) FireManagementInstrumentation(new ResourceConfigurationFailureEvent(instanceName, exception.ToString()));
			if (EventLoggingEnabled)
			{
				string errorMessage = string.Format(InternalResources.Culture, InternalResources.ErrorResourceConfigurationFailedMessage, instanceName);
				string entryText = eventLogEntryFormatter.GetEntryText(errorMessage, exception);

				EventLog.WriteEntry(GetEventSourceName(), entryText, EventLogEntryType.Error);
			}
		}

		/// <summary>
		/// Logs the occurrence of a general error for the Enterprise Library Resource Application Block through the 
		/// available instrumentation mechanisms.
		/// </summary>
		/// <param name="instanceName">Name of the <see cref="ResourceManager"/> instance in which the configuration error was detected.</param>
		/// <param name="exception">The exception raised for the configuration error.</param>
		public void LogError(string instanceName, Exception exception)
		{
			if (exception == null)
				throw new ArgumentNullException("exception");

			string errorMessage = string.Format(InternalResources.Culture, InternalResources.ErrorResourceOperationFailedMessage, instanceName);
			if (WmiEnabled) FireManagementInstrumentation(new ResourceFailureEvent(instanceName, errorMessage, exception.Message));
			if (EventLoggingEnabled)
			{
				string entryText = eventLogEntryFormatter.GetEntryText(errorMessage, exception);
				EventLog.WriteEntry(GetEventSourceName(), entryText, EventLogEntryType.Error);
			}
		}
		#endregion
	}
}